package com.centling.smartSealForPhone.application;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.app.Notification;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.centling.smartSealForPhone.BuildConfig;
import com.centling.smartSealForPhone.constants.SPConstants;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.ActivityCountManager;
import com.centling.smartSealForPhone.utils.CrashHandler;
import com.centling.smartSealForPhone.utils.LogUtil;
import com.centling.smartSealForPhone.utils.SPUtil;
import com.example.localalbum.common.LocalImageHelper;

import java.util.Locale;

import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;

/**
 * @author fionera
 */
public class SmartSeal
        extends Application {

    private static SmartSeal application;

    public static int screenWidth;
    public static int screenHeight;
    public static float screenDensity;
    public static float scaledDensity;

    public static SmartSeal getInstance() {
        return application;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        getDisplayMetrics();

        application = this;

        JPushInterface.init(this);
        JPushInterface.setDebugMode(BuildConfig.DEBUG);
        JPushInterface.stopCrashHandler(this);
        BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
        builder.notificationDefaults = Notification.DEFAULT_LIGHTS;
        JPushInterface.setDefaultPushNotificationBuilder(builder);

        UrlConstants.ROOTURL = "https://user.sealcloud.cn";
//        UrlConstants.ROOTURL = "https://portal.aliyun-c1.centling.cn:32272/zngz-test";
        UrlConstants.CLOUD_MAINTAIN_URL = "http://39.104.75.163:32185/smartseal-eye-0.1";

        if (!adjustMainProcess()) {
            return;
        }

        LocalImageHelper.init(application);

        if (!BuildConfig.DEBUG) {
            CrashHandler crashHandler = CrashHandler.getInstance();
            crashHandler.init(getApplicationContext());
        }

        LogUtil.d("screen width:" + screenWidth + "\nscreen height:" + screenHeight + "\nscreen " +
                "destiny:" + screenDensity);

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                ActivityCountManager.addActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                ActivityCountManager.removeActivity(activity);
            }
        });

    }

    private void getDisplayMetrics() {
        DisplayMetrics metric = getApplicationContext().getResources().getDisplayMetrics();
        screenWidth = metric.widthPixels;
        screenHeight = metric.heightPixels;
        screenDensity = metric.density;
        scaledDensity = metric.scaledDensity;
    }

    public boolean adjustMainProcess() {
        ActivityManager activityManager = (ActivityManager) getSystemService(
                Context.ACTIVITY_SERVICE);
        if (activityManager == null) {
            return false;
        }
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == android.os.Process.myPid()) {
                LogUtil.d("Current Process:" + appProcess.processName + " -> " + getPackageName());
                return TextUtils.equals(appProcess.processName, getPackageName());
            }
        }
        return false;
    }

    /**
     * public cloud ip is company id
     */
    public String getIp() {
        return SPUtil.getString(SPConstants.IPADDRESS);
    }

    public void changeIp(String ip) {
        SPUtil.setString(SPConstants.IPADDRESS, ip);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // 非默认值
        if (newConfig.fontScale != 1) {
            getResources();
        }
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        // 非默认值
        if (res.getConfiguration().fontScale != 1) {
            Configuration newConfig = res.getConfiguration();
            /*
            fontScale = 1 reset, {@link Configuration.setToDefaults() } Locale.equals 会导致空指针, 因为部分rom对于locale变量为空时没有判断null
             */
            newConfig.fontScale = 1;
            super.createConfigurationContext(newConfig);
        }
        return res;
    }
}

